home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / gscolor2.c < prev    next >
C/C++ Source or Header  |  1993-05-17  |  8KB  |  247 lines

  1. /* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gscolor2.c */
  20. /* Level 2 color and halftone operators for Ghostscript library */
  21. #include "gx.h"
  22. #include "gserrors.h"
  23. #include "gxfixed.h"            /* ditto */
  24. #include "gxmatrix.h"            /* for gzstate.h */
  25. #include "gxdevice.h"            /* for gx_color_index */
  26. #include "gxrefct.h"
  27. #include "gxcolor.h"            /* for gscolor2.h, gscie.h */
  28. #include "gscspace.h"            /* for gscolor2.h */
  29. #include "gscolor2.h"            /* for gscie.h */
  30. #include "gzstate.h"
  31. #include "gzcolor.h"
  32. #include "gscie.h"
  33.  
  34. /*
  35.  * NOTE: Ghostscript does not currently implement the Pattern color space.
  36.  * This is the module where that limitation is enforced,
  37.  * since setcolorspace is the only way that a non-Device color space
  38.  * can be made current (other than specialized operators such as setpattern.)
  39.  */
  40.  
  41. /* Define the Level 2 color space types. */
  42. #define cs_procs(scope, remap, install, adjust)\
  43.   scope cs_proc_remap_color(remap);\
  44.   scope cs_proc_install_cspace(install);\
  45.   scope cs_proc_adjust_count(adjust)
  46. cs_procs(extern, gx_remap_CIEBasedABC, gx_install_CIEBasedABC,
  47.   gx_adjust_CIEBasedABC);
  48. cs_procs(extern, gx_remap_CIEBasedA, gx_install_CIEBasedA,
  49.   gx_adjust_CIEBasedA);
  50. cs_procs(private, gx_remap_Separation, gx_install_Separation,
  51.   gx_adjust_Separation);
  52. cs_procs(private, gx_remap_Indexed, gx_install_Indexed,
  53.   gx_adjust_Indexed);
  54. cs_procs(private, gx_remap_Pattern, gx_install_Pattern,
  55.   gx_adjust_Pattern);
  56. const gs_color_space_type
  57.     gs_color_space_type_CIEBasedABC =
  58.      { gs_color_space_index_CIEBasedABC, 3,
  59.        gx_remap_CIEBasedABC, gx_install_CIEBasedABC, gx_adjust_CIEBasedABC
  60.      },
  61.     gs_color_space_type_CIEBasedA =
  62.      { gs_color_space_index_CIEBasedA, 1,
  63.        gx_remap_CIEBasedA, gx_install_CIEBasedA, gx_adjust_CIEBasedA
  64.      },
  65.     gs_color_space_type_Separation =
  66.      { gs_color_space_index_Separation, 1,
  67.        gx_remap_Separation, gx_install_Separation, gx_adjust_Separation
  68.      },
  69.     gs_color_space_type_Indexed =
  70.      { gs_color_space_index_Indexed, 1,
  71.        gx_remap_Indexed, gx_install_Indexed, gx_adjust_Indexed
  72.      },
  73.     gs_color_space_type_Pattern =
  74.      { gs_color_space_index_Pattern, -1,
  75.        gx_remap_Pattern, gx_install_Pattern, gx_adjust_Pattern
  76.      };
  77.  
  78. /* setcolorspace */
  79. int
  80. gs_setcolorspace(gs_state *pgs, gs_color_space *pcs)
  81. {    int code;
  82.     gs_color_space *pcs_old = pgs->color_space;
  83.     gs_client_color c;
  84.     if ( pgs->in_cachedevice )
  85.         return_error(gs_error_undefined);
  86.     switch ( pcs->type->index )
  87.     {
  88.     case gs_color_space_index_DeviceCMYK:
  89.         c.paint.values[3] = 1.0;
  90.     case gs_color_space_index_DeviceRGB:
  91.     case gs_color_space_index_CIEBasedABC:
  92.         c.paint.values[2] = c.paint.values[1] = 0.0;
  93.     case gs_color_space_index_DeviceGray:
  94.     case gs_color_space_index_CIEBasedA:
  95.     case gs_color_space_index_Indexed:
  96.         c.paint.values[0] = 0.0;
  97.         break;
  98.     case gs_color_space_index_Separation:
  99.         c.paint.values[0] = 1.0;
  100.         break;
  101.     /*case gs_color_space_index_Pattern:*/    /****** NYI ******/
  102.     default:
  103.         return gs_error_undefined;
  104.     }
  105.     if ( (code = (*pcs->type->adjust_count)(pcs, pgs, 1)) < 0 ||
  106.          (code = (*pcs_old->type->adjust_count)(pcs_old, pgs, -1)) < 0 ||
  107.          (*pcs_old = *pcs,
  108.           (code = (*pcs->type->install_cspace)(pcs, pgs)) < 0)
  109.        )
  110.         return code;
  111.     return gs_setcolor(pgs, &c);
  112. }
  113.  
  114. /* currentcolorspace */
  115. const gs_color_space *
  116. gs_currentcolorspace(const gs_state *pgs)
  117. {    return pgs->color_space;
  118. }
  119.  
  120. /* setcolor */
  121. int
  122. gs_setcolor(gs_state *pgs, const gs_client_color *pcc)
  123. {    const gs_color_space *pcs = pgs->color_space;
  124.     int code;
  125.     if ( pgs->in_cachedevice )
  126.         return_error(gs_error_undefined);
  127.     code = (*pcs->type->remap_color)(pcc, pcs, pgs->dev_color, pgs);
  128.     if ( code < 0 ) return code;
  129.     *pgs->ccolor = *pcc;
  130.     return 0;
  131. }
  132.  
  133. /* currentcolor */
  134. const gs_client_color *
  135. gs_currentcolor(const gs_state *pgs)
  136. {    return pgs->ccolor;
  137. }
  138.  
  139. /* setoverprint */
  140. void
  141. gs_setoverprint(gs_state *pgs, int ovp)
  142. {    pgs->overprint = ovp;
  143. }
  144.  
  145. /* currentoverprint */
  146. int
  147. gs_currentoverprint(const gs_state *pgs)
  148. {    return pgs->overprint;
  149. }
  150.  
  151. /* ------ Internal routines ------ */
  152.  
  153. /* Color remapping for Level 2 color spaces. */
  154. /* The CIE-based spaces are handled in gscie.c. */
  155.  
  156. private int
  157. gx_remap_Separation(const gs_client_color *pc, const gs_color_space *pcs,
  158.   gx_device_color *pdc, gs_state *pgs)
  159. {    float tint = pc->paint.values[0];
  160.     int code;
  161.     gs_client_color cc;
  162.     if ( tint < 0 ) tint = 0;
  163.     else if ( tint > 1 ) tint = 1;
  164.     code = (*pcs->params.separation.tint_transform)(tint, &cc.paint.values[0]);
  165.     if ( code < 0 ) return code;
  166.     return (*pcs->params.separation.alt_space.type->remap_color)(&cc,
  167.         (const gs_color_space *)&pcs->params.separation.alt_space,
  168.          pdc, pgs);
  169. }
  170.  
  171. private int
  172. gx_remap_Indexed(const gs_client_color *pc, const gs_color_space *pcs,
  173.   gx_device_color *pdc, gs_state *pgs)
  174. {    float value = pc->paint.values[0];
  175.     int index =
  176.         (value < 0 ? 0 :
  177.          value > pcs->params.indexed.hival ?
  178.            pcs->params.indexed.hival :
  179.          (int)value);
  180.     gs_client_color cc;
  181.     if ( pcs->params.indexed.use_proc )
  182.     {    int code = (*pcs->params.indexed.lookup.proc)(index, &cc.paint.values[0]);
  183.         if ( code < 0 ) return code;
  184.     }
  185.     else
  186.     {    int m = pcs->params.indexed.base_space.type->num_components;
  187.         const byte *pcomp = pcs->params.indexed.lookup.table + m * index;
  188.         int i;
  189.         for ( i = 0; i < m; i++, pcomp++ )
  190.             cc.paint.values[i] = *pcomp * (1.0 / 255.0);
  191.     }
  192.     return (*pcs->params.indexed.base_space.type->remap_color)(&cc,
  193.         (const gs_color_space *)&pcs->params.indexed.base_space,
  194.          pdc, pgs);
  195. }
  196.  
  197. private int
  198. gx_remap_Pattern(const gs_client_color *pc, const gs_color_space *pcs,
  199.   gx_device_color *pdc, gs_state *pgs)
  200. {    return gs_error_rangecheck;    /* NYI */
  201. }
  202.  
  203. /* Color space installation ditto. */
  204.  
  205. private int
  206. gx_install_Separation(gs_color_space *pcs, gs_state *pgs)
  207. {    return (*pcs->params.separation.alt_space.type->install_cspace)
  208.         ((gs_color_space *)&pcs->params.separation.alt_space, pgs);
  209. }
  210.  
  211. private int
  212. gx_install_Indexed(gs_color_space *pcs, gs_state *pgs)
  213. {    return (*pcs->params.indexed.base_space.type->install_cspace)
  214.         ((gs_color_space *)&pcs->params.indexed.base_space, pgs);
  215. }
  216.  
  217. private int
  218. gx_install_Pattern(gs_color_space *pcs, gs_state *pgs)
  219. {    if ( !pcs->params.pattern.has_base_space )
  220.         return 0;
  221.     return (*pcs->params.pattern.base_space.type->install_cspace)
  222.         ((gs_color_space *)&pcs->params.pattern.base_space, pgs);
  223. }
  224.  
  225. /* Reference count adjustment ditto. */
  226.  
  227. private int
  228. gx_adjust_Separation(gs_color_space *pcs, gs_state *pgs, int delta)
  229. {    return (*pcs->params.separation.alt_space.type->adjust_count)
  230.         ((gs_color_space *)&pcs->params.separation.alt_space, pgs, delta);
  231. }
  232.  
  233. private int
  234. gx_adjust_Indexed(gs_color_space *pcs, gs_state *pgs, int delta)
  235. {    return (*pcs->params.indexed.base_space.type->adjust_count)
  236.         ((gs_color_space *)&pcs->params.indexed.base_space, pgs, delta);
  237. }
  238.  
  239. private int
  240. gx_adjust_Pattern(gs_color_space *pcs, gs_state *pgs, int delta)
  241. {    /****** SHOULD ALSO REFCT Implementation ******/
  242.     if ( !pcs->params.pattern.has_base_space )
  243.         return 0;
  244.     return (*pcs->params.pattern.base_space.type->adjust_count)
  245.         ((gs_color_space *)&pcs->params.pattern.base_space, pgs, delta);
  246. }
  247.